﻿@using Gardener.Core.Client.Authorization
@using Gardener.Core.Client.Authorization.Events
@using Gardener.Core.Client.Shared
@using Gardener.Core.EventBus
@using Gardener.Core.Client.Constants
@using Gardener.Core.UserCenter.Dtos
@using Gardener.WoChat.Dtos.Notification;
<div class="wochat-show-box" style="@(userIsLogin?"":"display:none;")z-index: 999;">
    <Badge Count="messageCount">
        <Button Size="@ButtonSize.Large" Type="@ButtonType.Primary" Shape="@ButtonShape.Circle" Icon="comment" OnClick="OnClick" Style="font-size:34px;"></Button>
    </Badge>
</div>
@code {
    /// <summary>
    /// 最后的会话编号
    /// </summary>
    private Guid? lastSessionId;
    //用户登陆状态
    private bool userIsLogin = true;
    /// <summary>
    /// 消息数量
    /// </summary>
    private int messageCount = 0;
    /// <summary>
    /// 聊天窗体是否打开了
    /// </summary>
    private bool woCahtIsOpen = false;
    /// <summary>
    /// 身份权限服务
    /// </summary>
    [Inject]
    public IAuthenticationStateManager AuthenticationStateManager { get; set; } = null!;
    /// <summary>
    /// 操作框服务
    /// </summary>
    [Inject]
    private IOperationDialogService OperationDialogService { get; set; } = null!;
    /// <summary>
    /// 事件总线
    /// </summary>
    [Inject]
    private IEventBus EventBus { get; set; } = null!;


    /// <summary>
    /// 初始化
    /// </summary>
    /// <returns></returns>
    protected override void OnInitialized()
    {
        //模板页加载完成
        EventBus.Subscribe<MainInitializedEvent>(async token =>
        {
            bool ok = await AuthenticationStateManager.CheckCurrentUserHaveResourceAsync("global_wo_chat_btn");
            userIsLogin = ok;
            await InvokeAsync(StateHasChanged);
        });

        //登出成功
        EventBus.Subscribe<LogoutSucceedAfterEvent>(token =>
        {
            userIsLogin = false;
            return InvokeAsync(StateHasChanged);
        });
        //im消息
        EventBus.Subscribe<WoChatImUserMessageNotificationData>( message =>
        {
            //已打开聊天窗口就不统计了
            if (woCahtIsOpen) { return Task.CompletedTask; }
            UserDto? user = AuthenticationStateManager.GetCurrentUser();
            if (message.ImMessage.UserId != user?.Id)
            {
                messageCount++;
                lastSessionId = message.ImMessage.ImSessionId;
                return InvokeAsync(StateHasChanged);
            }
            return Task.CompletedTask;
        });
    }

    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    private async Task OnClick()
    {
        messageCount = 0;
        OperationDialogSettings dialogSettings = ClientConstant.DefaultOperationDialogSettings;
        dialogSettings.Width = 800;
        dialogSettings.BodyStyle = "padding: 5px;";
        await OperationDialogService.OpenAsync<WoChat, WoChatDialogConfig, bool>("WoChat", new WoChatDialogConfig()
            {
                DefaultSelectedSessionId = lastSessionId,
                Height = "65Vh"
            }, OnWoChatClose, dialogSettings);
        woCahtIsOpen = true;

    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="result"></param>
    /// <returns></returns>
    private Task OnWoChatClose(bool result)
    {
        woCahtIsOpen = false;
        return Task.CompletedTask;

    }
}
